<?php
//------------------------
// 前台购物车控制器
//-------------------------

namespace app\home\controller;

use app\common\controller\HomeBase;
use app\common\model\Cart as CartModel;



class Cart extends HomeBase
{
    protected $CartModel;




	protected function _initialize()
    {
        parent::_initialize();
        $this->CartModel              = new CartModel();

    }

    // 购物车列表页面
    public function index(){
        $cartInfo = $this->CartModel->getCartInfo();

        // halt($cartInfo);
        $this->assign('cartInfo',$cartInfo);

        return $this->fetch('index');
    }

    // 添加到购物车
    public function addToCart(){
        $data = $this->request->param('');

        $addData = array(
            'goods_id'       => $data['goods_id'],
            'goods_spec_key' => $data['goods_spec_key'],
            'quantity'       => $data['quantity']
        );

        $this->CartModel->addToCart($addData);

        $msg = $data['goods_name'] . "添加到购物车成功";
        $this->success($msg);
    }

    // 更新商品数量
    public function updateData(){
        $data = $this->request->param('');

        $updateData = array(
            'goods_id'       => $data['goods_id'],
            'goods_spec_key' => $data['goods_spec_key'],
            'quantity'       => $data['quantity']
        );

        $this->CartModel->updateData($updateData);

        // $msg = $data['goods_name'] . "添加到购物车成功";
        // $this->success($msg);
    }

    // 从购物车删除
    public function delToCart(){
        $cart_id = $this->request->param('id',0,'intval');
        $goods_spec_key = $this->request->param('goods_spec_key','','trim');

        $res = $this->CartModel->delToCart($cart_id,$goods_spec_key);

        if ($res) {
            $this->success("删除成功");
        }
    }

    // 清空购物车
    public function emptyCart(){
        $res = $this->CartModel->emptyCart();
        $this->success("购物车已清空");
    }

    // 获取购物车数据
    public function abc(){
        return 123;
    }





}
